import { dayNewlyAddedAnnouncementUsingPost } from '@/services/backend/announcementController';
import { dayNewlyAddedFileUsingPost } from '@/services/backend/fileInfoController';
import { dayNewlyAddedUserUsingPost } from '@/services/backend/userController';
import { Col, notification, Row } from 'antd';
import { useEffect, useState } from 'react';
import ChartCard from '../ChartCard';

const topColResponsiveProps = {
  xs: 24,
  sm: 12,
  md: 12,
  lg: 12,
  xl: 6,
  style: {
    marginBottom: 24,
  },
};

const IntroduceRow = () => {
  const [userCount, setUserCount] = useState<number>(0);
  const [announcementCount, setAnnouncementCount] = useState<number>(0);
  const [fileCount, setFileCount] = useState<number>(0);
  const [otherCount, setOtherCount] = useState<number>(0);

  const loadUserStatistics = async () => {
    try {
      const { data, code } = await dayNewlyAddedUserUsingPost();
      if (code === 0 && data) {
        const count = data.reduce((accumulator, item) => {
          return accumulator + Number(item?.num ?? 0);
        }, 0);
        setUserCount(count);
      }
    } catch (e: any) {
      notification.error({
        message: '系统异常',
        description: e.message,
      });
    }
  };
  const loadAnnouncementStatistics = async () => {
    try {
      const { data, code } = await dayNewlyAddedAnnouncementUsingPost();
      if (code === 0 && data) {
        const count = data.reduce((accumulator, item) => {
          return accumulator + Number(item?.num ?? 0);
        }, 0);
        setAnnouncementCount(count);
      }
    } catch (e: any) {
      notification.error({
        message: '系统异常',
        description: e.message,
      });
    }
  };
  const loadFileStatistics = async () => {
    try {
      const { data, code } = await dayNewlyAddedFileUsingPost();
      if (code === 0 && data) {
        const count = data.reduce((accumulator, item) => {
          return accumulator + Number(item?.num ?? 0);
        }, 0);
        setFileCount(count);
      }
    } catch (e: any) {
      notification.error({
        message: '系统异常',
        description: e.message,
      });
    }
  };
  const loadOtherStatistics = async () => {
    try {
      // const { data, code } = await dayNewlyAddedOtherUsingPost();
      // if (code === 0 && data) {
      //   const count = data.reduce((accumulator, item) => {
      //     return accumulator + Number((item?.num ?? 0));
      //   }, 0);
      setOtherCount(-1);
      // }
    } catch (e: any) {
      notification.error({
        message: '系统异常',
        description: e.message,
      });
    }
  };

  //页面加载时的钩子
  useEffect(() => {
    //加载数据
    loadUserStatistics();
    loadOtherStatistics();
    loadFileStatistics();
    loadAnnouncementStatistics();
  }, []);

  return (
    <Row gutter={24}>
      <Col {...topColResponsiveProps}>
        <ChartCard
          style={{
            backgroundColor: 'rgba(132,250,176,0.77)',
          }}
          bordered={false}
          title="系统总人数"
          total={userCount}
          contentHeight={46}
        ></ChartCard>
      </Col>

      <Col {...topColResponsiveProps}>
        <ChartCard
          style={{
            backgroundColor: 'rgba(79,191,168,0.72)',
          }}
          bordered={false}
          title="系统公告数"
          total={announcementCount}
          contentHeight={46}
        ></ChartCard>
      </Col>
      <Col {...topColResponsiveProps}>
        <ChartCard
          style={{
            backgroundColor: 'rgba(143,229,244,0.73)',
          }}
          bordered={false}
          title="文件数"
          total={fileCount}
          contentHeight={46}
        ></ChartCard>
      </Col>
      <Col {...topColResponsiveProps}>
        <ChartCard
          style={{
            backgroundColor: 'rgba(143,211,244,0.72)',
          }}
          bordered={false}
          title="其他数"
          total={otherCount}
          contentHeight={46}
        ></ChartCard>
      </Col>
    </Row>
  );
};
export default IntroduceRow;
